package com.zhongli.devplatform.controller.teacher;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongli.devplatform.bean.SecurityHelper;
import com.zhongli.devplatform.dto.teacher.ReadOperationDTO;
import com.zhongli.devplatform.dto.student.StuCommitOperaDTO;
import com.zhongli.devplatform.po.common.ReadClazzOperationPO;
import com.zhongli.devplatform.po.common.ReadClazzPO;
import com.zhongli.devplatform.po.common.ReadCommitOperationPO;
import com.zhongli.devplatform.po.common.ReadGradePO;
import com.zhongli.devplatform.po.teacher.ReadOperationPO;
import com.zhongli.devplatform.po.user.ReadUserPO;
import com.zhongli.devplatform.po.user.ReadUserParentPO;
import com.zhongli.devplatform.po.view.ViewUserPO;
import com.zhongli.devplatform.service.imp.common.ReadClazzService;
import com.zhongli.devplatform.service.imp.common.ReadGradeService;
import com.zhongli.devplatform.service.imp.common.ReadTeachClazzService;
import com.zhongli.devplatform.service.imp.common.ReadClazzOperationService;
import com.zhongli.devplatform.service.imp.student.ReadCommitOperationService;
import com.zhongli.devplatform.service.imp.student.ReadUserParentService;
import com.zhongli.devplatform.service.imp.teacher.ReadOperationService;
import com.zhongli.devplatform.service.imp.user.ReadUserService;
import com.zhongli.devplatform.utils.BeanCopierUtil;
import com.zhongli.devplatform.utils.DateUtil;
import com.zhongli.devplatform.utils.user.UserUtil;
import com.zhongli.devplatform.vo.student.OperationCommitVO;
import com.zhongli.devplatform.vo.teacher.OperationVO;
import com.zhongli.devplatform.bean.Res;
import com.zhongli.devplatform.vo.teacher.TeacherOperationVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author wy
 * @since 2020/7/29
 */
@Api(tags = "老师作业app接口",description = "老师作业app接口",value = "老师作业app接口")
@RestController
@RequestMapping("/operation")
public class OperationController {

    @Autowired
    private ReadOperationService operationService;

    @Autowired
    private ReadCommitOperationService commitOperationService;

    @Autowired
    private ReadClazzService clazzService;

    @Autowired
    private ReadTeachClazzService teachClazzService;

    @Autowired
    private ReadClazzOperationService clazzOperationService;

    @Autowired
    private ReadGradeService gradeService;

    @ApiOperation("老师发布作业/或者修改")
    @PostMapping("/save")
    public Res homeworkPublish(@RequestBody ReadOperationDTO operationDTO){
        operationService.saveOpera(operationDTO);
        return Res.ok("success");
    }

    @ApiOperation("获取当前老师的所有未批改的作业个数")
    @GetMapping("/getUnCheckCount")
    public Res getUnCheckCount(){
        Integer currentUserId = SecurityHelper.getCurrentUserId();
        return commitOperationService.getUnCheckCount(currentUserId);
    }

    @ApiOperation("获取当前老师的所有布置的作业")
    @GetMapping("/currentAllOpera")
    public Res<List<OperationVO>> getCurrentTeacherOperation(){
        Integer currentUserId = SecurityHelper.getCurrentUserId();
        List<OperationVO> data = operationService.getCurrentTeacherOperation(currentUserId);
        data.forEach(this::judgmentStatus);
        return Res.ok(data);
    }

    @ApiOperation("根据id查询老师布置的作业")
    @GetMapping("/getOperationById")
    public Res<OperationVO> getOperationById(@RequestParam("operationId") Integer operationId,@RequestParam("clazzId") Integer clazzId){

        ReadOperationPO operationPO = operationService.getById(operationId);
        ReadClazzOperationPO clazzOperationPO = clazzOperationService.getOne(new QueryWrapper<ReadClazzOperationPO>()
                .eq("operation_id", operationPO.getId()).eq("clazz_id",clazzId));
        OperationVO operationVO = BeanCopierUtil.copy(operationPO, OperationVO.class);
        if (!Objects.isNull(clazzOperationPO)){
            ReadClazzPO clazzPO = clazzService.getById(clazzId);
            ReadGradePO gradePO = gradeService.getById(clazzPO.getGradeId());
            String operationPOImges = operationPO.getImges();
            String[] split = operationPOImges.split(",");
            assert operationVO != null;
            operationVO.setImgs(Arrays.asList(split));
            operationVO.setClazzId(clazzPO.getId());
            operationVO.setClassName(clazzPO.getClassName());
            operationVO.setGradeId(gradePO.getId());
            operationVO.setGradeName(gradePO.getGradeName());
        }
        return Res.ok(operationVO);
    }

    @ApiOperation("获取当前老师的所有布置的作业(批改作业)")
    @GetMapping("/getTeacherOperation/{status}")
    public Res<List<TeacherOperationVO>> getTeacherOperation(@PathVariable("status") Integer status){
        Integer currentUserId = SecurityHelper.getCurrentUserId();
        //已批改的学生人数
        List<TeacherOperationVO> data = operationService.getTeacherOperation(currentUserId,status);
        return Res.ok(data);
    }


    /**
     * 设置作业的开始状态
     * @param item  OperationVO
     */
    private void judgmentStatus(OperationVO item){
        Date now = new Date();
        Date startTime = item.getStartTime();
        Date endTime = item.getEndTime();
        Calendar startInstance = Calendar.getInstance();
        startInstance.setTime(startTime);
        Calendar endInstance = Calendar.getInstance();
        endInstance.setTime(endTime);
        if (DateUtil.isEffectiveDate(now, startTime, endTime)){
            item.setWhetherToStart(2);
        }else if (Calendar.getInstance().before(startInstance)){
            item.setWhetherToStart(1);
        }else if (Calendar.getInstance().after(endInstance)){
            item.setWhetherToStart(3);
        }
    }

}
